<template>
  <!-- 导航 -->
  <div>
    <van-nav-bar
      title="登录"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
  </div>
  <div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <!-- 表单 -->
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <!-- 登录按钮 -->
        <van-button
          round
          block
          type="primary"
          native-type="submit"
          @click="login"
        >
          登录
        </van-button>
        <p @click="goreg">还没有账号?点我注册吧</p>
      </div>
    </van-form>
  </div>
</template>
<script setup>
import router from "@/router";
import http from "@/utils/http";
import { ref } from "vue";
// 导航栏回退
const onClickLeft = () => history.back();

const username = ref("");
const password = ref("");

const onSubmit = (values) => {
  console.log("submit", values);
};
// 登录
function login() {
  http({
    url: "",
    method: "post",
    data: {
      username: username.value,
      password: password.value,
    },
  }).then((v) => {
    console.log(v);
    if (v.data.code == 1) {
      // 存储token和id
      localStorage.setItem("token", v.data.token);
      localStorage.setItem("uid", v.data.user.id);
      showNotify({ type: "success", message: "登录成功!" });
      router.push("/");
    } else {
      showNotify({ type: "warning", message: "v.data.message" });
    }
  });
}
// 点击之后跳转注册页面
function goreg() {
  router.push("/Register");
}
</script>
<style lang=""></style>
